﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using log4net;
using CashFlow.Db.DAL;
using CashFlow.Db.Entity;

namespace SysTool._02_Service
{
    public class OrderService
    {
        private static ILog _logger = LogManager.GetLogger(typeof(OrderService));

        public static IList<OrderAndUserInfo> QueryAllUsersOrders(string date)
        {
            var users = UserService.QueryAllUsersByUserId(UserService.LoginUserInfo.Id);
            var userIds = users.Select(x => x.Id).ToList();
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls = db.Queryable<OrderEntity>()
                    .InnerJoin<UserInfoEntity>((o, u) => o.BuyerUserId == u.Id)
                    .Where((o, u) => userIds.Contains(u.Id) && o.CreateTime.ToString().Substring(0, 10) == date)
                    .Select((o, u) => new OrderAndUserInfo
                    {
                        OrderInfo = o,
                        UserInfo = u
                    }).ToList();
                return ls;
            }
        }

        public static IList<OrderBuySellInfo> QueryShopAllOrders(string date)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls = db.Queryable<OrderEntity>()
                    .InnerJoin<UserInfoEntity>((o, u1) => o.BuyerUserId == u1.Id)
                    .InnerJoin<UserInfoEntity>((o, u1, u2) => o.SellerUserId == u2.Id)
                    .InnerJoin<ShopUserEntity>((o, u1, u2, su) => o.BuyerUserId == su.UserId)
                    .Where((o, u1, u2, su) => o.CreateTime.ToString().Substring(0, 10) == date
                            && (su.ShopManagerUserId == UserService.LoginUserInfo.Id))
                    .Select((o, u1, u2, su) => new OrderBuySellInfo
                    {
                        Order = o,
                        Buyer = u1,
                        Seller = u2
                    })
                    .ToList();
                return ls;
            }
        }

        public static IList<OrderAndUserInfo> QueryShopAllUserOrders(string date)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls = db.Queryable<OrderEntity>()
                    .InnerJoin<ShopUserEntity>((o, su) => o.BuyerUserId == su.UserId)
                    .InnerJoin<UserInfoEntity>((o, su, u) => o.BuyerUserId == u.Id)
                    .Where((o, su, u) => o.CreateTime.ToString().Substring(0, 10) == date)
                    .Select((o, su, u) => new OrderAndUserInfo
                    {
                        OrderInfo = o,
                        UserInfo = u
                    }).ToList();
                return ls;
            }
        }

        public static OrderEntity QueryByOrderNo(string orderNo)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                return db.Queryable<OrderEntity>().Where(x => x.OrderNo == orderNo).First();
            }
        }

        public static bool UpdateByOrderNo(OrderEntity order)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                int n = db.Updateable(order).ExecuteCommand();
                return n > 0;
            }
        }

        public static bool AddOrder(OrderEntity order)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                int n = db.Insertable(order).ExecuteCommand();
                return n > 0;
            }
        }

        public static bool BatchDelete(IList<string> Ids)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                int n = db.Deleteable<OrderEntity>(Ids.ToArray()).ExecuteCommand();
                return n > 0;
            }
        }

        public static bool DeleteById(int Id)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                int n = db.Deleteable<OrderEntity>(Id).ExecuteCommand();
                return n > 0;
            }
        }
    }
}
